package cn.com.cfae.iras.doc.analyze.parse.extractor;

import cn.com.cfae.iras.doc.analyze.parse.model.*;
import cn.com.cfae.iras.doc.analyze.parse.tokenizer.QYZXTokenizer;
import cn.com.cfae.iras.doc.analyze.parse.tokenizer.SXTokenizer;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class M7Extractor extends MTableExtractor {
    private static final Logger logger = LoggerFactory.getLogger(M7Extractor.class);

    private QYZXTokenizer qyzxTokenizer = QYZXTokenizer.getInstance();

    private SXTokenizer sxTokenizer = SXTokenizer.getInstance();

    @Override
    public void extract() {
        try {
            extractM7();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private void extractM7() {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取{}章业务模型数据......", getChapterModel().getChapterId());
        try {
            parseChapterSection();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        try {
            extractM7Xypjjg2();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        try {
            extractM7Sx();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章业务模型数据完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }

    private void extractM7Xypjjg2() {
        ExtractBizModel extractBizModel = getExtractBizModel("XYPJJG");
        qyzxTokenizer.loadCoreSynonyms(extractBizModel);
        List<SectionModel> sectionModelList = getChapterModel().getSectionModelList();
        if (CollectionUtils.isEmpty(sectionModelList)) {
            logger.error("抽取{}章，章信用评级机构业务模型数据，请确认！！！", getChapterModel().getChapterId());
            return;
        }
        for (int i = 0; i < sectionModelList.size(); i++) {
            SectionModel sectionModel = sectionModelList.get(i);
            List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
            if (CollectionUtils.isEmpty(childSectionModelList)) {
                continue;
            }
            for (int j = 0; j < childSectionModelList.size(); j++) {
                SectionModel childSectionModel = childSectionModelList.get(j);
                if (childSectionModel == null) {
                    continue;
                }
                String content = childSectionModel.getContent();
                if (isEmpty(content)) {
                    continue;
                }
                logger.info("抽取{}章信用评级机构业务模型数据，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), extractBizModel.getModelName(), childSectionModel.getTitle());
                qyzxTokenizer.splitWords3(extractBizModel, childSectionModel);
            }
        }
        qyzxTokenizer.unloadCoreSynonyms(extractBizModel);
    }

    private void extractM7Sx() {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取{}章授信业务模型数据......", getChapterModel().getChapterId());
        extractSX_SXZE();
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章授信业务模型数据完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }

    /**
     * 煤炭-授信总额、已使用授信
     */
    private void extractSX_SXZE() {
        ExtractBizModel extractBizModel = getExtractBizModel("SX");
        sxTokenizer.loadCoreSynonyms(extractBizModel);
        List<SectionModel> sectionModelList = getChapterModel().getSectionModelList();
        if (CollectionUtils.isEmpty(sectionModelList)) {
            logger.error("抽取{}章业务模型数据，分析内容为空，请确认！！！", getChapterModel().getChapterId());
            return;
        }
        for (int i = 0; i < sectionModelList.size(); i++) {
            SectionModel sectionModel = sectionModelList.get(i);
            List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
            if (CollectionUtils.isEmpty(childSectionModelList)) {
                continue;
            }
            for (int j = 0; j < childSectionModelList.size(); j++) {
                SectionModel childSectionModel = childSectionModelList.get(j);
                if (childSectionModel == null) {
                    continue;
                }
                String content = childSectionModel.getContent();
                if (isEmpty(content)) {
                    continue;
                }
                logger.info("抽取{}章业务模型数据，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), extractBizModel.getModelName(), childSectionModel.getTitle());
                sxTokenizer.splitWords5(extractBizModel, content);
            }
        }
        sxTokenizer.unloadCoreSynonyms(extractBizModel);
    }
}
